package com.twt.sys.controller;

import com.twt.sys.interceptor.NotLoginException;
import com.twt.model.system.entity.SysUser;
import com.twt.sys.base.BaseController;
import com.twt.common.pojo.Result;
import com.twt.model.base.SysConsts;
import com.twt.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    // 这些对象何以直接被子类使用
    protected HttpServletRequest request;
    protected HttpServletResponse response;
    protected HttpSession session;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest req, HttpServletResponse res) {
        this.request = req;
        this.response = res;
        this.session = req.getSession();
    }

    @GetMapping("currentUser")
    public Result<SysUser> currentUser() {
        SysUser loginUser = (SysUser) session.getAttribute(SysConsts.SYSUSER_KEY);
        if (loginUser == null) {
            throw new NotLoginException();
        }
        return Result.ok(loginUser);
    }

    @GetMapping("registrySysUser")
    public Result<SysUser> registrySysUser(String account, String password) {
        SysUser sysUser = sysUserService.registrySysUser(account, password);
        return Result.ok(sysUser);
    }

}
